package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 签到记录实体类 - 修正版
 */
@Getter
@Setter
@ToString(exclude = {"activity", "images"})
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "check_in_records")
public class CheckInRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "check_date", nullable = false)
    @NotNull(message = "打卡日期不能为空")
    private LocalDate checkDate;

    @Column(name = "check_time")
    private LocalTime checkTime;

    @Column(name = "description")
    private String description;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "activity_id", nullable = false)
    @NotNull(message = "活动不能为空")
    private CheckInActivity activity;

    // 添加与CheckInImage的一对多关系
    @OneToMany(mappedBy = "record", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @Builder.Default
    private List<CheckInImage> images = new ArrayList<>();

    // 添加获取图片URL列表的便捷方法
    @Transient
    public List<String> getImageUrls() {
        return images.stream()
                .map(CheckInImage::getImageUrl)
                .toList();
    }

    // 添加图片的方法
    public void addImage(CheckInImage image) {
        images.add(image);
        image.setRecord(this);
    }

    public void removeImage(CheckInImage image) {
        images.remove(image);
        image.setRecord(null);
    }
}
